package com.all.play.myadapterdrag;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.all.play.myadapterdrag.adapter.MyAdapter;
import com.all.play.myadapterdrag.bean.QQMessage;
import com.all.play.myadapterdrag.callback.MyItemTouchHelperCallBack;
import com.all.play.myadapterdrag.data.DataUtils;
import com.all.play.myadapterdrag.interfaces.StartDragListener;
import com.all.play.myadapterdrag.view.MyItemDecoration;

import java.util.List;

public class MainActivity extends AppCompatActivity implements StartDragListener{

    private RecyclerView recyclerView;
    //private List<String> list;
    private MyAdapter adapter;
    private ItemTouchHelper itemTouchHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initview();

        initData();
    }

    private void initData() {
        List<QQMessage> list = DataUtils.init();


        adapter = new MyAdapter(this, list);

        recyclerView.setAdapter(adapter);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(new MyItemDecoration(this,LinearLayoutManager.VERTICAL));
        //条目触摸监听
        MyItemTouchHelperCallBack callback = new MyItemTouchHelperCallBack(this,adapter);

        //帮助类
        itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }

    @Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder){
        itemTouchHelper.startDrag(viewHolder);
    }

    private void initview() {
        recyclerView = (RecyclerView) findViewById(R.id.rv);

//        list = new ArrayList<>();
//        for (int i=0;i<20;i++){
//            list.add("hashiqi "+i);
//        }
//        Log.i("onBindViewHolder", "onBindViewHolder: "+ list.size());

    }
}
